/*
 * Giffel, Android application
 *
 * Copyright (C) 2011  Mid Sweden University
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package se.appening;

import java.io.InputStream;
import java.net.URL;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

public class InfoActivity extends Activity implements TextToSpeech.OnInitListener {

    private int entryId = -1;

    private TextToSpeech textToSpeach;
    private String textToSay = "";

    private Entry displayedEntry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);

        if (MainActivity.controller == null) {
            MainActivity.controller = new MainController(this, new MockCringleGetter());
        }
        
        Entry[] entries = MainActivity.controller.getEntries();

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            entryId = extras.getInt("entry");
        }

        Log.e("sdf", "EID:" + entryId);

        if (entries.length >= entryId) {
            Entry entry = entries[entryId];
            displayedEntry = entry;

            TextView infoTitle = (TextView) findViewById(R.id.infotitle);
            infoTitle.setText(entry.getTitle());

            ImageView infoThumb = (ImageView) findViewById(R.id.infofigure);
            infoThumb.setImageBitmap(loadBitmapImage(entry.getImageUrl()));

            // TODO: Should be updating...
            TextView infoDistance = (TextView) findViewById(R.id.infodistance);
            infoDistance.setText(String.valueOf(entry.getDistance()));

            TextView infoType = (TextView) findViewById(R.id.infotype);
            infoType.setText(entry.getType());

            TextView infoDescription = (TextView) findViewById(R.id.infodescription);
            infoDescription.setText(entry.getDescription());

            TextView infoVote = (TextView) findViewById(R.id.infovote);
            infoVote.setText("Likes: " + entry.getVotes());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Text to Speech");
        menu.add("Map");
        menu.add("Like");

        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        String s = item.getTitle().toString();

        if (s.equalsIgnoreCase("Text to Speech")) {
            TextView descriptionView = (TextView) this.findViewById(R.id.infodescription);
            textToSay = descriptionView.getText().toString();

            textToSpeach = new TextToSpeech(this, this);

            return true;
        }

        if (s.equalsIgnoreCase("Map")) {
            Intent newActivity = new Intent(this, MapActivity.class);

            Location userLoc = MainActivity.controller.getCurrentLocation();
            double userLat = userLoc.getLatitude();
            double userLong = userLoc.getLongitude();

            Location itemLoc = displayedEntry.location;
            double itemLat = itemLoc.getLatitude();
            double itemLong = itemLoc.getLongitude();

            newActivity.putExtra("userLat", userLat);
            newActivity.putExtra("userLong", userLong);
            newActivity.putExtra("itemLat", itemLat);
            newActivity.putExtra("itemLong", itemLong);

            startActivity(newActivity);
        }

        if (s.equalsIgnoreCase("Like")) {
            displayedEntry.vote();

            TextView infoVote = (TextView) findViewById(R.id.infovote);
            infoVote.setText("Likes: " + displayedEntry.getVotes());
            infoVote.postInvalidate();
        }

        return super.onMenuItemSelected(featureId, item);
    }

    private Bitmap loadBitmapImage(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            return BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            return BitmapFactory.decodeResource(this.getResources(), R.drawable.empty);
        }
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = textToSpeach.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA ||
                    result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TAG", "Language is not available.");
            } else {
                textToSpeach.speak(textToSay, TextToSpeech.QUEUE_FLUSH, null);
            }
        } else {
            Log.e("TAG", "Could not initialize TextToSpeech.");
        }

    }

}